package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;

import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UtilContext;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    //处理查询所有部门的请求,响应HTML
    //login/*.do
    @RequestMapping("/emplogin")
    @ResponseBody
    public JsonResult list(String username, String password, HttpSession session) {

            Employee employee = employeeService.login(username, password);
            //session.setAttribute("USER_IN_SESSION", employee);
            UtilContext.setCurrentUser(employee);
            //根据员工id查询对应的权限,(返回类型要是个字符串,不能存对象进去)
            List<String> permissions = employeeService.selectByEidPermission(employee.getId());
            //session.setAttribute("EXPRESSION_IN_SESSION",permissions);
            UtilContext.setExpression(permissions);
            return new JsonResult();

    }


    @RequestMapping("/logout")
    public String list(HttpSession session) {
        //删除session
        session.invalidate();
        //重定向到登录界面
        return "redirect:login.html";
    }

    @RequestMapping("/noexpression")
    public String noexpression() {
        //跳转到没有权限页面
        return "common/nopermission";
    }

}


